(function () {  
    function Unit(options) {
        this.x = options.x || 2;
        this.y = options.y || 2;
        this.width = options.width || 20;
        this.height = options.height || 20;
        this.color = options.color || "yellow";
    };
    // 渲染單元
    Unit.prototype.render = function (map, x, y) {
        // 生成隨機坐標，需要保證地圖寬度大於食物寬度
        // var maxX = map.offsetWidth / this.width ;
        // var maxY = map.offsetHeight / this.height ;
        // this.x = Tools.getRandom(0,maxX)-1;
        // this.y = Tools.getRandom(0,maxY)-1;
        // 生成新元素
        if (arguments.length > 1) {
            this.x = x;
            this.y = y;
        }
        var div = document.createElement("div");
        div.style.left = this.x * this.width + "px";
        div.style.top = this.y * this.height + "px";
        div.style.width = this.width + "px";
        div.style.height = this.height + "px";
        div.style.position = "absolute";
        div.style.backgroundColor = this.color;
        this.div = div;
        map.appendChild(div);
    };
    Unit.prototype.repaint = function () {
        this.div.style.left = this.x * this.width + "px";
        this.div.style.top = this.y * this.height + "px";
    }
    // 移除單元
    Unit.prototype.remove = function (map) {
        map.removeChild(this.div);
    }
    window.Unit=Unit;
})();

